// cartStore
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { Api } from '@/apis/index'
export const useCartStore = defineStore('cart', () => {
    const cartList = ref([])
    const total = ref(0)
    const totalNum = ref(0)
    const otherPrice = ref(0)
    const getCartList = () => {
        Api.getCartList().then((res) => {
            cartList.value = res.data.cartList
            total.value = res.data.total
            totalNum.value = res.data.totalNum
            otherPrice.value = res.data.otherPrice
        }).catch((err) => {
            // console.log(err)
            throw err
        })
    }

    const addToCart = (product) => {
        const index = cartList.value.indexOf(product)
        if (!cartList.value.includes(product)) {
            cartList.value.push(product)
        }
        else {
            cartList.value[index].count++
            if (product.onCart) {
                total.value = total.value + product.price * 1
                totalNum.value = totalNum.value + 1
            }
        }
    }

    const removeFromCart = (product) => {
        const index = cartList.value.indexOf(product)
        if (index > -1) {
            cartList.value[index].count--
            if (cartList.value[index].count === 0) {
                cartList.value.splice(index, 1)
            }
            if (product.onCart) {
                total.value = total.value - product.price
                totalNum.value = totalNum.value - 1
                if (totalNum.value <= 0) {
                    otherPrice.value = 0
                }
            }
        } else {
            console.log('Product not found in cart')
        }
    }

    const setOnCart = (product, bool) => {
        const index = cartList.value.indexOf(product)
        if (index > -1) {
            cartList.value[index].onCart = bool
            if (bool) {
                total.value = total.value + product.price * product.count
                totalNum.value = totalNum.value + product.count
            }
            else {
                total.value = total.value - product.price * product.count
                totalNum.value = totalNum.value - product.count
                if (totalNum.value <= 0) {
                    otherPrice.value = 0
                }
            }
        } else {
            console.log('Product not found in cart')
        }
        console.log(total.value);

    }

    const selectAll = (bool) => {
        total.value = 0
        totalNum.value = 0

        if (bool) {
            cartList.value.forEach((product) => {
                product.onCart = true
                total.value = total.value * 1 + product.price * product.count
                totalNum.value = totalNum.value * 1 + product.count * 1
            })
            if (totalNum.value > 0) {
                otherPrice.value = 20
            }
        }
        else {
            otherPrice.value = 0
            cartList.value.forEach((product) => {
                product.onCart = false
            })
        }
    }

    const clearCart = () => {
        cartList.value = []
        total.value = 0
        totalNum.value = 0
    }

    return {
        cartList,
        getCartList,
        addToCart,
        removeFromCart,
        total,
        setOnCart,
        totalNum,
        otherPrice,
        selectAll,
        clearCart
    }
})